#-*- coding:UTF-8 -*-
'''
Created on 2012-7-19
Thread test
@author: Administrator
'''
import threading
from time import ctime, sleep

class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args
    
    def getResult(self):
        return self.res
    
    def run(self):
        print 'starting', self.name, 'at:', ctime()
        self.res = apply(self.func, self.args)
        print self.name, 'finished at:', ctime()  

def fib(x):
    sleep(0.005)
    if x < 2:return 1
    return (fib(x - 2) + fib(x - 1))

def fac(x):
    sleep(0.1)
    if x < 2:return 1
    return (x * fac(x - 1))

def sumNum(x):
    sleep(0.1)
    if x < 2:return 1
    return (x + sumNum(x - 1))

funcs = [fib, fac, sumNum] 
n = 12

def main():
    threads = []
    for func in funcs:
        t = MyThread(func, (n,), func.__name__)
        threads.append(t)
    
    for myThread in threads:
        myThread.start()
    
    for myThread in threads:
        myThread.join()
        print myThread.getResult()
    print '执行完毕 '

if __name__ == '__main__':
    main()
        
        
